package com.ican.dao.product;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.product.Option;

@Repository("optionDao")
public class OptionDao extends HibernateDao<Option, Integer> {

    public void createOption(){
        String sql = "{call p_product_option()}";
        this.createSqlQuery(sql).executeUpdate();
    }
    
    public Page<Option> findAllOption2Syn(Page<Option> page) {
        return findPage(page, "from Option where nameCode is null");
    }
    
    public List<Option> findAllOption(String name, Integer langId) {
        return find("from Option where optionName = '" + name + "' and langId = " + langId);
    }
    
    public void updateOptionName(Integer id, String nameNew) {
        String hql = "update Option set optionName = '" + nameNew + "' where id = " + id;
        this.batchExecute(hql);
    }
    
    public void updateOptionCode(Integer id, String nameCode, Integer ocOptionId) {
        String hql = "update Option set nameCode = '" + nameCode + "', ocOptionId = " + ocOptionId + " where id = " + id;
        this.batchExecute(hql);
    }
    
    public Option findOptionByNameAndLang(String optionName, Integer langId){
        String hql = "from Option where optionName = '" + optionName + "' and langId = " + langId;
        return this.findUnique(hql);
    }
    
    public Option findOptionByNameCodeAndLang(String nameCode, Integer langId){
        String hql = "from Option where nameCode = '" + nameCode + "' and langId = " + langId;
        return this.findUnique(hql);
    }
}
